package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.dao.AreaDAO;
import cj.web.admin.dao.ComposeOnlineOrderDAO;
import cj.web.admin.dao.ComposeSubjectDAO;
import cj.web.admin.dao.ComposeUserRefDAO;
import cj.web.admin.dao.GameDAO;
import cj.web.admin.dao.GradeDAO;
import cj.web.admin.dao.SchoolDAO;
import cj.web.admin.dao.UserInfoDAO;
import cj.web.admin.domain.ComposeOnlineOrder;
import cj.web.admin.domain.ComposeSubject;
import cj.web.admin.domain.ComposeUserRef;
import cj.web.admin.domain.UserInfo;
import cj.web.constants.GameTypeEnum;
import cj.web.constants.IsCheckEnum;
import cj.web.constants.IsExamEnum;
import cj.web.constants.IsWinEnum;
import cj.web.constants.MemuEnum;
import cj.web.constants.PayStatusEnum;
import cj.web.constants.PayWayEnum;
import cj.web.constants.UserTypeEnum;
import cj.web.login.controllers.BaseController;

@Path("/userRef")
public class COnlineUserRefController extends BaseController{

	private static Logger logger = Logger.getLogger(COnlineUserRefController.class.getName());
	
	@Autowired
	private UserInfoDAO userInfoDao;
	@Autowired
	private ComposeUserRefDAO composeUserRefDao;
	@Autowired
	private ComposeOnlineOrderDAO composeOnlineOrderDao;
	@Autowired
	private GradeDAO gradeDao;
	@Autowired
	private AreaDAO areaDao;
	@Autowired
	private SchoolDAO schoolDao;
	@Autowired
	private GameDAO gameDao;
	@Autowired
	private ComposeSubjectDAO composeSubjectDAO;
	
	@Get("show")
	public String show(Model model) {
		model.add("MemuEnum", MemuEnum.wlzwfpsj);
		
		return "/views/online/userRefList.vm";
	}

	@Post("getList")
	public String queryList(UserInfo userInfo) {
		JSONObject jo = new JSONObject();
		
		//查询阅卷老师
		userInfo.setType(UserTypeEnum.teacher.getValue());
		List<UserInfo> obj = userInfoDao.queryByType(userInfo);
		
		int size = userInfoDao.querySizeByType(userInfo);
		
		jo.put("data", obj);
		jo.put("draw", userInfo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("showCmp")
	public String showCmp(Model model, @Param("id")int userId) {
		model.add("MemuEnum", MemuEnum.wlzwfpsj);
		model.add("userId", userId);
		UserInfo teacher = userInfoDao.queryById(userId);
		model.add("teacherName", teacher.getName());
		
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		List<Map> games = gameDao.asGameEnum(Integer.parseInt(GameTypeEnum.WLZZ.getValue()));
		model.add("Game", games);
		model.add("IsCheckStatus", EnumSet.allOf(IsCheckEnum.class));
		model.add("IsExamStatus", EnumSet.allOf(IsExamEnum.class));
		
		
		return "/views/online/composeList.vm";
	}
	
	@Post("getSubjects")
	public String getSubjects(@Param("gameId")int gameId){
		JSONObject jo = new JSONObject();
		List<ComposeSubject> subject = composeSubjectDAO.queryByGameId(gameId);
		jo.put("subjects", subject);
		
		return "@"+jo.toString();
	}
	
	@Post("getCmpList") //试卷分配
	public String getCmpList(ComposeOnlineOrder composeOnlineOrder) {
		JSONObject jo = new JSONObject();
		
		composeOnlineOrder.setStatus(PayStatusEnum.YFK.getValue());  //已付款订单的考卷才能分配
		composeOnlineOrder.setIsWin(-1);
		List<ComposeOnlineOrder> objs = composeOnlineOrderDao.queryQuestion(composeOnlineOrder);
		
		for(ComposeOnlineOrder obj:objs){
			obj.setIsCheckName(IsCheckEnum.getName(obj.getIsCheck()));
			obj.setIsExamName(IsExamEnum.getName(obj.getIsExam()));
		}
		
		int size = composeOnlineOrderDao.queryQuestionSize(composeOnlineOrder);
		jo.put("data", objs);
		jo.put("draw", composeOnlineOrder.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("mutiFinish")
	public String mutiFinish(@Param("userId")int userId, @Param("id")List<Integer> orderIds, HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		String acount = returnSessionObj(request).getAccount();
		
		composeUserRefDao.create(orderIds, userId, acount);
		composeOnlineOrderDao.updateTeacherId(orderIds, userId, acount);
		
		return "@"+jo.toString();
	}
	
	@Post("resetDist")//重新设置为未分配
	public String resetDist(@Param("id")int id, @Param("userId")int teacherId){
		JSONObject jo = new JSONObject();
		
		composeOnlineOrderDao.rmTeacherId(id); //去掉订单中的阅卷老师id
		composeUserRefDao.delete(id, teacherId);      //删除引用关系
		
		return "@"+jo.toString();
	}
	
	/**
	 * 分配管理
	 * @param model
	 * @return
	 */
	@Get("showfpgl")
	public String showfpgl(Model model) {
		model.add("MemuEnum", MemuEnum.wlzwfpgl);
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		List<Map> games = gameDao.asGameEnum(Integer.parseInt(GameTypeEnum.WLZZ.getValue()));
		model.add("Game", games);
		model.add("IsCheckStatus", EnumSet.allOf(IsCheckEnum.class));
		model.add("IsExamStatus", EnumSet.allOf(IsExamEnum.class));
		UserInfo userInfo = new UserInfo();
		userInfo.setAccount("");
		userInfo.setType(UserTypeEnum.teacher.getValue());
		List<UserInfo> obj = userInfoDao.queryByType(userInfo);
		model.add("teacherlis", obj);
		
		return "/views/online/composeFpglList.vm";
	}
	
	@Post("getCmpFpglList") //试卷分配
	public String getCmpFpglList(ComposeOnlineOrder composeOnlineOrder) {
		JSONObject jo = new JSONObject();
		
		composeOnlineOrder.setStatus(PayStatusEnum.YFK.getValue());  //已付款订单的考卷才能分配
		composeOnlineOrder.setIsWin(-1);
		List<ComposeOnlineOrder> objs = composeOnlineOrderDao.queryFpglQuestion(composeOnlineOrder);
		
		for(ComposeOnlineOrder obj:objs){
			obj.setIsCheckName(IsCheckEnum.getName(obj.getIsCheck()));
			obj.setIsExamName(IsExamEnum.getName(obj.getIsExam()));
		}
		
		int size = composeOnlineOrderDao.queryFpglQuestionSize(composeOnlineOrder);
		jo.put("data", objs);
		jo.put("draw", composeOnlineOrder.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}

}